<!--
 * @Description: 砍价活动商品管理列表
 * @Author: liyujie
 * @Date: 2021/04/19
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/04/19
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    砍价活动 - 商品管理
                </div>
                <div class="add-btn">
                    <el-button
                        type="primary"
                        size="medium"
                        icon="el-icon-plus"
                        @click="handleRoute('bargainGoodsEdit', {activity_no: filterData.activity_no, bargain_no: '', type: 'add'})"
                    >
                        添加内容
                    </el-button>
                </div>
            </div>
            <!-- /header -->
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        商品信息
                    </p>
                    <el-input
                        placeholder="请输入搜索内容"
                        v-model="filterData.keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        style="width: 240px"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                </div>
            </div>
            <!--筛选-->

            <!-- table -->
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                    ref="tempLateName"
                    class="data-table"
                    v-loading="tableLoading"
                >
                    <el-table-column
                        label="序号"
                        align="center"
                        width="50"
                        type="index"
                    />
                    <el-table-column
                        label="商品缩略图/红包描述"
                        align="center"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        width="150"
                    >
                        <template slot-scope="scope">
                            <div v-if="scope.row.aim_type === 'goods'">
                                <img
                                    :src="scope.row.goods_cover"
                                    style="width: 60px;cursor: pointer"
                                    @click="fnShowPreview(scope.row.goods_cover)"
                                >
                            </div>
                            <div v-if="scope.row.aim_type === 'platform_coupon' || scope.row.aim_type === 'merchant_coupon'">
                                {{ scope.row.coupon_desc }}
                            </div>
                            <div v-if="scope.row.aim_type === 'red_packet'">
                                {{ scope.row.red_packet_desc }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="aim_no"
                        label="商品/优惠券/红包编号"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        width="160"
                    />
                    <el-table-column
                        label="类型"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <div v-if="scope.row.aim_type === 'goods'">商品</div>
                            <div v-if="scope.row.aim_type === 'platform_coupon'">平台优惠券</div>
                            <div v-if="scope.row.aim_type === 'merchant_coupon'">商家优惠券</div>
                            <div v-if="scope.row.aim_type === 'red_packet'">平台红包</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="sort"
                        show-overflow-tooltip
                        label="排序"
                        align="center"
                    />
                    <el-table-column
                        label="原价格"
                        align="center"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <div v-if="scope.row.aim_type === 'goods'">{{ scope.row.goods_price_origin }}</div>
                            <div v-else>--</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="活动价"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <div v-if="scope.row.aim_type === 'goods'">{{ scope.row.price }}</div>
                            <div v-else>--</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="stock"
                        label="库存"
                        show-overflow-tooltip
                        :width="120"
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="apply_count"
                        label="发起砍价的次数"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        :width="120"
                        align="center"
                    />
                    <el-table-column
                        prop="success_count"
                        label="砍价成功的次数"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        :width="120"
                        align="center"
                    />
                    <el-table-column
                        prop="require_bargain_count"
                        label="砍价刀数"
                        show-overflow-tooltip
                        :width="120"
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        label="状态"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <div class="status-wrapper">
                                <div
                                    class="status-dot"
                                    :class="['status-dot', scope.row.status === 1 ? 'success' : 'normal']"
                                />
                                <div>{{ scope.row.status | statusFilter }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        align="left"
                        width="180"
                        fixed="right"
                    >
                        <template slot-scope="scope">
                            <div class="option-btn-group">
                                <el-button
                                    type="text"
                                    @click="handleRoute('bargainGoodsEdit', {activity_no: filterData.activity_no, bargain_no: scope.row.bargain_no, type: 'edit'})"
                                >
                                    编辑
                                </el-button>
                                <el-button
                                    type="text"
                                    @click="handleRoute('bargainActivityInitiatorList', {bargain_no: scope.row.bargain_no})"
                                >
                                    查看参与用户
                                </el-button>
                                <el-button
                                    type="text"
                                    @click="handleDelete(scope.row)"
                                >
                                    删除
                                </el-button>
                                <el-button
                                    type="text"
                                    :disabled="scope.row.status === 0"
                                    @click="handleSetInvalid(scope.row)"
                                >
                                    失效
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- /table -->

            <!-- /分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>

        <!-- 图片预览 -->
        <img-preview
            ref="preview"
        />
    </div>
</template>

<script>
import { back } from '@/mixin/back';
import ImgPreview from "@/components/common/img-preview";

export default {
    name: 'BargainGoodsList',
    mixins: [back],
    components: {ImgPreview},
    data() {
        return {
            filterData: {
                activity_no: '',
                keyword: '',
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            authResult: {}
        };
    },
    computed: {},
    watch: {},
    filters: {
        statusFilter(val) {
            switch (val) {
                case 0:
                    return '已失效';
                case 1:
                    return '进行中';
                case 2:
                    return '已售罄';
                default:
                    return '--';
            }
        }
    },
    mounted() {
        this.filterData.activity_no = this.$route.query.activity_no;
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '砍价活动'}, {title: '活动管理', path: '/student/bargain-activity-list'}, {title: '商品管理'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            this.$post('/student/shop_tool_bargain%5Cbargain%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 预览图片
         * @param   {String}    images      //单图预览
         * @param   {Array}     images      //多图预览
         * @param index
         */
        fnShowPreview(images, index) {
            if (images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images, index);
            } else {
                this.$message({
                    type: 'warning',
                    message: '图片类型错误'
                });
            }
        },

        /**
         * 删除
         * @param row
         */
        handleDelete(row) {
            if(!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$confirm('是否要删除当前商品?', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/shop_tool_bargain%5Cbargain%5Cdelete', {bargain_no: row.bargain_no}, resp => {
                    if (resp.code === 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            duration: 2000,
                            type: 'success'
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            }).catch(() => {});
        },

        /**
         * 失效
         * @param row
         */
        handleSetInvalid(row) {
            if(!this.authResult.auth_invalid) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$confirm('是否要设置当前商品为失效状态?', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/shop_tool_bargain%5Cbargain%5Cdisable', {bargain_no: row.bargain_no}, resp => {
                    if (resp.code === 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            duration: 2000,
                            type: 'success'
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            }).catch(() => {});
        },
    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;

        /*header*/
        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        /*筛选*/

        .filter-view {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            padding: 0 24px;
            margin-top: 12px;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 12px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
            }
        }

        // 表格
        .table-wrapper {
            padding: 0 24px;
            flex: 1;
            height: 0;
        }

        .status-wrapper {
            display: flex;
            align-items: center;
            justify-content: center;

            .status-dot {
                width: 6px;
                height: 6px;
                border-radius: 50%;
                margin-right: 5px;

                &.success {
                    background: #4CD964;
                }

                &.danger {
                    background: #FF3030;
                }

                &.warning {
                    background: #FFA72D;
                }

                &.normal {
                    background: #C2C2C2;
                }
            }
        }

        /* 排序 */
        .sort-wrap {
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            .sort-spliter {
                width: 1px;
                height: 12px;
                background: rgba(235, 235, 235, 1);
                margin: 0 10px;
            }
            .btn-sort {
                font-size: 14px;
                line-height: 1;
                color: #ccc;
                &:hover {
                    color: #20a0ff;
                }
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding: 12px 24px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
    }
    /deep/ .el-textarea__inner {
        resize: none;
    }
    .data-table {
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
        // 操作按钮
        .option-btn-group {
            .el-button {
                margin-left: 0;
                margin-right: 10px;
            }
        }
    }
}
</style>
